import { defineStore } from "pinia"

export type Tab = {
    title: string
    path: string
}

// state的数据类型
export type TabState = {
    tabList: Tab[]
}

export const tabStore = defineStore('tabStore', {
    state: (): TabState => {
        return {
            tabList: []
        }
    },
    getters: {
        getTab(state) {
            return state.tabList
        }
    },
    actions: {
        addTab(tab: Tab) {
            // 去重
            if (this.tabList.some(item => item.path === tab.path)) return
            if (tab.path === '/dashboard') {
                this.tabList.unshift(tab)
            } else {
                this.tabList.push(tab)
            }
        },
        clear() {
            this.tabList = []
        }
    },
    persist: {
        storage: {
            getItem(key) {
                return sessionStorage.getItem(key)
            },
            setItem(key, value) {
                sessionStorage.setItem(key, value)
            }
        }
    }
    // persist: { // 持久化
    //     enabled: true,
    //     strategies: [
    //         { storage: sessionStorage, paths: ['tabList'] }
    //     ]
    // }
})